package com.wan.activity

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import com.wan.R
import com.wan.adapter.CommonlyAdapter
import com.wan.base.BaseBean
import com.wan.entity.CommonlyEntity
import com.wan.uitls.*
import com.zhy.view.flowlayout.FlowLayout
import com.zhy.view.flowlayout.TagFlowLayout
import kotlinx.android.synthetic.main.activity_commonly_used.*
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory


/**
 * 常用网站
 */
class CommonlyUsedActivity : AppCompatActivity(), TagFlowLayout.OnTagClickListener {

    override fun onTagClick(view: View?, position: Int, parent: FlowLayout?): Boolean {
        var entity = dataList[position]
        CommonUtils.sendIntent(this,WebActivity::class.java,entity.link,entity.name)
        return true
    }




    private val mThreadName = this.javaClass.name

    private val dataList = ArrayList<CommonlyEntity>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_commonly_used)

        initView()
    }

    private fun initView() {
        var adapter = CommonlyAdapter(this, dataList)
        commonly_tfl.adapter = adapter
        commonly_tfl.setOnTagClickListener(this)
        var manager = ApiManager(this)
        manager.getCommonlyWebSite().enqueue(object : Callback<BaseBean<ArrayList<CommonlyEntity>>> {
            override fun onFailure(call: Call<BaseBean<ArrayList<CommonlyEntity>>>, t: Throwable) {
                Log.e(mThreadName, "onFailure : 失败了==>${call.execute().body()}", t)
            }

            override fun onResponse(call: Call<BaseBean<ArrayList<CommonlyEntity>>>, response: Response<BaseBean<ArrayList<CommonlyEntity>>>) {
                Log.d(mThreadName, "onResponse : 成功==>${response.body()}")
                var arrayList = response.body()!!.data
                dataList.addAll(arrayList)
                adapter.notifyDataChanged()
            }
        })
    }

    override fun onDestroy() {
        super.onDestroy()
        RetrofitHelper.get().cancelCall(this)
    }
}
